﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data.Factory;
using System;
using System.Collections.Generic;
using TDM.DbData.DataEntity;

namespace TDM.WebSvr.Controllers.TestData
{
    [AllowAnonymous]
    public class TestSubTaskController : NXDO.WebAPI.Controllers.ApiDbEntityController<TestSubTask, long>
    {
        protected override string GetPkName()
        {
            return "STaskID";
        }

        protected override string GetTableName()
        {
            return "TestSubTask";
        }

        public override bool Delete(long pkValue)
        {
            var sql = $"update {GetTableName()} set IsDelete = 1 where {GetPkName()} = {pkValue}";
            return DbHelper.SetUpdateCommand(sql).ExecuteNonQuery() > 0;
        }

        public override List<bool> DeleteCollection([FromBody] List<long> pkValues)
        {
            List<bool> flags = new List<bool>();
            foreach (var pkValue in pkValues)
            {
                var sql = $"update {GetTableName()} set IsDelete = 1 where {GetPkName()} = {pkValue}";
                var flag = DbHelper.SetUpdateCommand(sql).ExecuteNonQuery() > 0;
                flags.Add(flag);
            }
            return flags;
        }

        public override TestSubTask Post([FromBody] TestSubTask data)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            data.CreateUID = uid;
            data.CreateTime = DateTime.Now;
            return base.Post(data);
        }

        public override List<TestSubTask> PostCollection([FromBody] List<TestSubTask> datas)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            datas.ForEach(data =>
            {
                data.CreateTime = DateTime.Now;
                data.CreateUID = uid;
            });
            return base.PostCollection(datas);
        }

        /// <summary>
        /// 复制子任务
        /// </summary>
        /// <param name="STaskID">源ID</param>
        /// <param name="Count">数量</param>
        [HttpGet]
        public void ExeCopySubTask(long STaskID, int Count)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            WebBiz.TestData.TestSubTaskHelper.ExeCopySubTask(STaskID, Count, uid);
        }

        /// <summary>
        /// 查询任务包含的子任务
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<TestSubTask> GetTestSubTasks(long taskid)
        {
            try
            {
                return WebBiz.TestData.TestSubTaskHelper.GetTestSubTasks(taskid);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
